package org.lionsoul.websnail.sample;

import org.lionsoul.websnail.Spider;
import org.lionsoul.websnail.SpiderOptions;
import org.lionsoul.websnail.downloader.Downloader;
import org.lionsoul.websnail.downloader.Proxy;
import org.lionsoul.websnail.downloader.WebDriverProcessor;
import org.lionsoul.websnail.downloader.webdriver.DefaultWebDriverBuilder;
import org.lionsoul.websnail.downloader.webdriver.SnailWebDriver;
import org.lionsoul.websnail.downloader.webdriver.WebDriverBuilder;
import org.lionsoul.websnail.downloader.webdriver.WebDriverDownloader;
import org.lionsoul.websnail.store.RedisTodo;
import org.lionsoul.websnail.store.RedisVisited;
import org.lionsoul.websnail.task.Task;
import org.lionsoul.websnail.task.TaskFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;

/**
 * 爬虫启动程序
 *
 * @author RockYang
 */
public class BlogSpider
{

    public static void main(String[] args) throws InterruptedException
    {
        customTaskSimple();
    }

    // 默认爬虫
    private static void simple() throws InterruptedException
    {
        SpiderOptions options = SpiderOptions.custom()
                .setConnectTimeout(10000)
                .setAnalyzerFactory(BlogAnalyzer::new)
                .setDownloader(Downloader.HTTP_CLIENT)
                .setTaskInterval(1000).build();
        Spider spider = new Spider(options);
        spider.addSeedUrl("https://www.r9it.com");
        spider.start();
    }

    // 使用 redis 作为 todolist 存储介质
    private static void redisSimple() throws InterruptedException
    {
        SpiderOptions options = SpiderOptions.custom()
                .setConnectTimeout(10000)
                .setAnalyzerFactory(BlogAnalyzer::new)
                .setTodoHandler(new RedisTodo("127.0.0.1", 6379, 0, "spider_todo_list"))
                .setVisitedHandler(new RedisVisited("127.0.0.1", 6379, 0, "spider_visited"))
                .setDownloader(Downloader.HTTP_CLIENT)
                .setTaskInterval(1000).build();
        Spider spider = new Spider(options);
        spider.addSeedUrl("https://www.r9it.com");
        spider.start();
    }

    // 创建自定义的 Downloader
    private static void customDownloaderSimple() throws InterruptedException
    {
        ChromeOptions chromeOptions = new ChromeOptions();
        // 创建自定义 downloader
        WebDriverBuilder builder = new DefaultWebDriverBuilder(1, chromeOptions);

        builder.setCondition(ExpectedConditions.presenceOfElementLocated(By.className("global-ui")));
        builder.setSelector(By.id("app")); // 抓取 ID 为 app 元素的 InnerHTML
        // 创建一个 WebDriverDownloader
        WebDriverDownloader downloader = new WebDriverDownloader(builder);

        // 给 WebDriver 对象设置后置处理器，进行一些初始化设置，例如设置 cookie 等
        downloader.setDriverProcessor(new WebDriverProcessor()
        {
            @Override
            public void initialize(SnailWebDriver driver)
            {
                // 设置窗口全屏
                driver.getWebDriver().manage().window().fullscreen();
            }

            @Override
            public void beforeProcess(SnailWebDriver driver)
            {

            }
        });
        // 创建 HttpDownLoader
        //downloader = new HttpClientDownloader();
        SpiderOptions options = SpiderOptions.custom()
                .setConnectTimeout(10000)
                .setAnalyzerFactory(BlogAnalyzer::new)
                .setDownloader(downloader)
                .setTaskInterval(1000).build();
        Spider spider = new Spider(options);
        spider.addSeedUrl("https://www.r9it.com");
        spider.start();
    }

    // 设置爬虫代理
    private static void proxySimple() throws InterruptedException
    {
        SpiderOptions options = SpiderOptions.custom()
                .setConnectTimeout(10000)
                .setAnalyzerFactory(BlogAnalyzer::new)
                .setProxy(Proxy.getHttpsProxy("127.0.0.1", 7777))
                .setTaskInterval(1000).build();
        Spider spider = new Spider(options);
        spider.addSeedUrl("https://www.r9it.com");
        spider.start();
    }

    // 实现自定义的采集任务
    private static void customTaskSimple() throws InterruptedException
    {
        ChromeOptions chromeOptions = new ChromeOptions();
        WebDriverBuilder builder = new DefaultWebDriverBuilder(1, chromeOptions);
        WebDriverDownloader downloader = new WebDriverDownloader(builder);
        downloader.setDriverProcessor(new WebDriverProcessor()
        {
            @Override
            public void initialize(SnailWebDriver driver)
            {

            }

            @Override
            public void beforeProcess(SnailWebDriver driver)
            {
                try {
                    // 添加 cookie
                    driver.getWebDriver().manage().addCookie(new Cookie("username", "user1"));
                    // 全屏
                    //driver.getWebDriver().manage().window().fullscreen();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        SpiderOptions options = SpiderOptions.custom()
                .setConnectTimeout(10000)
                .setDownloader(downloader)
                .setTaskFactory(new TaskFactory()
                {
                    @Override
                    public Task createTask(Spider spider, String url)
                    {
                        return new CustomTask(spider, url);
                    }
                })
                .setDownloader(Downloader.WEB_DRIVER)
                .setTaskInterval(3000).build();
        Spider spider = new Spider(options);
        spider.addSeedUrl("https://list.jd.com/list.html?cat=9987,653,655");
        spider.start();
    }

}
